package ChatPackage;

import java.util.Vector;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import androidApplication.javaFiles.ChatAlert;
import androidApplication.javaFiles.ChatList;
import androidApplication.javaFiles.UsersNameList;

/*
 * this class represent the current chat the user see   
 */
public class ChatHandler 
{


	//***************************************Class Variables*******************************************//
	private static Vector<Chat> _chats = new Vector<Chat>();
	private static Chat _currentChat = null;

	//***************************************Public Methods*******************************************//

	public static synchronized void Set_currentChat(Chat newChat){
		_currentChat = newChat;
	}

	public static synchronized Chat get_currentChat() {
		return _currentChat;
	}

	public static synchronized void startChat(String userId, Context context){
		Log.v("ChatHandler","in startChat");
		Chat answer = isTheChatExists(userId);
		_currentChat = answer;
		startChatListActivity(context);
	}


	public static synchronized void handleChatMessage(Context context, String senderID,String msg){
		//check if the message is from the current chat and the activity is running
		if(get_currentChat()!=null && _currentChat.getUserAndID().equals(senderID)){
				continueChatListActivity(context,msg);
		}
		else{//if no check if their is a chat to this sender if no create one and add the message to the chat
			Chat chat = isTheChatExists(senderID);
			chat.addNewMessage(msg);
			startChatAlert(context,senderID);
		}
	}

	

	public static synchronized Vector<String> getUsersList(){
		Vector<String> ans = new Vector<String>();
		for(Chat chat:_chats){
			ans.add(chat.getUserAndID());	
		}
		return ans;
	}
	
	/**
	 * starting the UsersList Activity after chats button was pushed
	 */
	public static void startUsersListActivity(Context context) {
		Intent intent = new Intent(context,UsersNameList.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		context.startActivity(intent);
	}
	//***************************************Private Methods*******************************************//

	private static void continueChatListActivity(Context currentCon,String message_content){
		Intent intent = new Intent(currentCon,ChatList.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra("message",message_content);
		currentCon.startActivity(intent);
	}

	private static void startChatListActivity(Context context) {
		Intent intent = new Intent(context,ChatList.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		//TODO add the user id selected to exstras
		context.startActivity(intent);
	}
	
	/**
	 * calling the chat alert activity for new message who arrived
	 * @param context the current context
	 * @param senderID the sender of the message
	 */
	private static void startChatAlert(Context context,String senderID) {
		
		Intent intent = new Intent(context, ChatAlert.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra("sender",senderID);
		context.startActivity(intent);
	}
	
	

	/**
	 * search if the chat already exist if yes returns it otherwise create new and returns it 
	 * @param deviceId of the other user in chat 
	 * @return
	 */
	private static Chat isTheChatExists(String deviceId){
		Chat chat = null;
		boolean found = false;
		for (Chat iterable : _chats) {
			if(iterable.getUserAndID().equals(deviceId)){
				chat = iterable;
				found = true;
			}
		}
		if(!found){
			chat = new Chat(deviceId);
			_chats.add(chat);
		}
		return chat;
	}


	//***************************************Protected Methods*******************************************//	

}
